<?php

namespace Frieza\Modules\App\Controllers;

use App\Http\Controllers\Controller;
use App\Pockets\GridDataModel as pGridDataModel;
use Doraemon\model\Position\PositionBaseInfo;
use Doraemon\model\Position\PositionRules;
use Doraemon\service\position\AdmOperationModule;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Widgets\Tab;
use Illuminate\Http\Request;
use Illuminate\Support\MessageBag;

/**
 * Class BottomNavController
 * @package Frieza\Modules\App\Controllers
 */
class BottomNavController extends Controller
{
    const ICON_WIDTH = 50;
    private $grid = null;
    private $iconLink = true;

    public function content(Content $content)
    {

        $tab = new Tab();
        $tab->addLink('icon列表', route('app.bottomNav'), $this->iconLink);
        $tab->addLink('链接配置', route('app.bottomNavConf'), !$this->iconLink);
        $tab->addLink('气泡配置', '/admin/position/singleBanner/32', false);
        $header = 'APP底部导航' . ($this->iconLink ? 'icon配置' : '链接配置');
        return $content
            ->header($header)
            ->description(' ')
            ->breadcrumb(['text' => $header])
            ->body($tab)
            ->row($this->grid);
    }

    public function sort(Request $request)
    {
        $sortInfo = $request->input('sort');
        AdmOperationModule::saveBottomNavSort($sortInfo);
    }

    public function delete($id)
    {
        AdmOperationModule::delBottomNav($id);
    }

    public function bottomNavConf(Request $request, Content $content)
    {
        $this->iconLink = false;

        $list = AdmOperationModule::getBottomNavLinkList();
        $gridModel = new pGridDataModel($list, count($list), ['perPage' => $pageSize = 5]);

        $this->grid = Admin::grid($gridModel, function (Grid $grid) {
            $grid->disableRowSelector();
            $grid->disableExport();
            $grid->disablePagination();
            $grid->disableFilter();
            $grid->column('id', 'ID')->sort();
            $grid->column('title', '名称');
            $grid->column('link', '链接')->linkSelect(false);
            $grid->column('u', '分群信息')->userProfile();
        });

        return $this->content($content);
    }

    public function bottomNavConfCreate(Content $content, $id = 0)
    {
        $info = AdmOperationModule::getBottomNav($id);
        $form = new Form($info);
        $form->action(route('app.bottomNavConfSave'));
        $form->hidden('id');
        $form->text('title', '名称');
        $form->multiLinkSelect('link', '跳转链接');
        $form->userProfile('u', '用户分群')->generate();
        $header = $id > 0 ? '修改APP底部导航链接配置' : '添加APP底部导航链接配置';
        return $content->header($header)->description(' ')->breadcrumb(
            ['text' => 'APP底部导航链接配置列表', 'url' => route('app.bottomNavConf')],
            ['text' => $header]
        )->row($form);
    }

    public function bottomNavConfSave(Request $request)
    {
        $id = $request->input('id', '');
        $title = $request->input('title', '');
        $link = $request->input('link', []);
        $link_type = $link['link_type'] ?? '';
        $link_params = $link['link_params'] ?? [];
        $link_params = json_encode($link_params);

        try {

            $data = compact('title', 'link_type', 'link_params');

            $newId = AdmOperationModule::saveBottomNavConf($data, $id);

            //添加分群信息
            $userProfile = $request->input('u', []);

            AdmOperationModule::addUserProfile($newId, $userProfile, PositionRules::MODULE_TYPE_BOTTOM_NAV);

            $success = new MessageBag([
                'title' => ($id > 0 ? '编辑' : '添加') . '成功'
            ]);

            return redirect(route('app.bottomNavConf'))->with(compact('success'));

        } catch (\Exception $e) {

            $error = new MessageBag([
                'title' => '操作失败,信息:' . $e->getMessage()
            ]);

            return back()->withInput()->with(compact('error'));

        }
    }


    public function bottomNav(Request $request, Content $content)
    {
        $list = AdmOperationModule::getBottomNavList();
        $gridModel = new pGridDataModel($list, count($list), ['perPage' => $pageSize = 5]);

        $this->grid = Admin::grid($gridModel, function (Grid $grid) {
            $width = self::ICON_WIDTH;
            $grid->disableRowSelector();
            $grid->disableExport();
            $grid->disablePagination();
            $grid->disableFilter();
            $grid->column('id', 'ID')->sort();
            $grid->column('title', '名称');
            $grid->column('festival', '类型')->using(AdmOperationModule::bottomNavFestivalOptions());
            $grid->column('dark', '色系')->using(AdmOperationModule::bottomNavColorOptions());
            $grid->column('show_desc', '当前状态及展示时间')->display(function ($title, Grid\Column $column) {
                return $this->show_desc;
            });
            $grid->column('icon_url', 'icon')->image('', $width, $width);
            $grid->column('icon_sel_url', 'sel_icon')->image('', $width, $width);
        });

        return $this->content($content);
    }

    public function addBottomNav(Request $request, Content $content, $id = 0)
    {
        $info = AdmOperationModule::getBottomNav($id);
        $form = new Form($info);
        $form->action(route('app.saveBottomNav'));
        $form->hidden('id');
        $form->text('title', '名称');
        $form->radio('festival', '节日icon')->options(AdmOperationModule::bottomNavFestivalOptions());
        $form->radio('dark', '色系')->options(AdmOperationModule::bottomNavColorOptions());
        $form->imgUpload('icon_id', 'icon', true);
        $form->imgUpload('icon_sel_id', '选中时的icon', true);

        $form->fieldChange('show_type', '展示时间', $form)
            ->options(PositionBaseInfo::SHOW_TYPE_MAP)
            ->default(PositionBaseInfo::SHOW_TYPE_TIME)
            ->bgColor('')
            ->required()
            ->callableMap([
                PositionBaseInfo::SHOW_TYPE_TIME => function (Form $form) {
                    $form->dateTimeRange('start_show_time', 'end_show_time', ' ')->required();
                },
                PositionBaseInfo::SHOW_TYPE_ALWAYS => function (Form $form) {
                    $form->datetime('start_show_time2','开始展示时间')->required();
                },
            ]);
        $title = 'APP底部导航icon配置';
        $header = ($id ? '修改' : '新增') . $title;
        return $content->header($header)->description(' ')->breadcrumb(
            ['text' => $title . '列表', 'url' => route('app.bottomNav')],
            ['text' => $header]
        )->row($form);
    }

    public function saveBottomNav(Request $request)
    {
        $id = $request->input('id', '');
        $title = $request->input('title', '');
        $icon_id = $request->input('icon_id', '');
        $icon_sel_id = $request->input('icon_sel_id', '');
        $festival = $request->input('festival', '');
        $dark = $request->input('dark', '');
        $show_type = $request->input('show_type', PositionBaseInfo::SHOW_TYPE_TIME);

        if ($show_type == PositionBaseInfo::SHOW_TYPE_TIME) {
            $start_show_time = $request->input('start_show_time', '');
            $end_show_time = $request->input('end_show_time', '');
        } else {
            $start_show_time = $request->input('start_show_time2', '');
        }
        try {

            $data = compact('title', 'dark', 'festival', 'icon_id', 'icon_sel_id', 'show_type', 'start_show_time', 'end_show_time');

            AdmOperationModule::saveBottomNav($data, $id);

            $success = new MessageBag([
                'title' => ($id > 0 ? '编辑' : '添加') . '成功'
            ]);

            return redirect(route('app.bottomNav'))->with(compact('success'));

        } catch (\Exception $e) {

            $error = new MessageBag([
                'title' => '操作失败,信息:' . $e->getMessage()
            ]);

            return back()->withInput()->with(compact('error'));

        }
    }

}
